//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.ft.adbix.oaid;

import android.content.Context;
import android.os.Build.VERSION;
import android.text.TextUtils;

import com.ft.adbix.oaid.util.OAIDLogUtil;
import com.ft.adbix.oaid.util.OAIDUtils;
import com.ft.adbix.utils.AdLogUtils;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy;

public class OAIDManager {
    private static volatile OAIDManager instance;
    private String mOAID;
    private String mVAID;
    private String mAAID;
    private final ThreadPoolExecutor threadPoolExecutor = this.createThreadPoolExecutor();
    private boolean isDebug;
    private boolean isCanUseOaid;
    private Context context;
    private OAIDUtils.OnOAIDLoadCallBack onOAIDLoadCallback;

    public static OAIDManager getInstance() {
        if (instance == null) {
            synchronized (OAIDManager.class) {
                if (instance == null) {
                    instance = new OAIDManager();
                }
            }
        }

        return instance;
    }

    private OAIDManager() {
    }

    public void setCertPath(String path) {
    }

    public void init(Context context, OAIDUtils.OnOAIDLoadCallBack onOAIDLoadCallback, boolean isCanUseOaid) {
        this.init(context, onOAIDLoadCallback, isCanUseOaid, true);
    }

    public void init(Context context, OAIDUtils.OnOAIDLoadCallBack onOAIDLoadCallback,boolean isCanUseOaid, boolean isDebug) {
        this.context = context;
        this.onOAIDLoadCallback = onOAIDLoadCallback;
        this.isCanUseOaid = isCanUseOaid;
        this.isDebug = isDebug;
        if (isCanUseOaid) {
            initOaid();
        }
    }

    public void initOaid() {
        if (!this.isCanUseOaid) {
            AdLogUtils.d("Oaid同步获取失败 : 不允许SDK使用oaid信息，不进行oaid初始化");
        } else if (VERSION.SDK_INT < 26) {
            AdLogUtils.d("OAID同步获取失败 : 8.0以下系统无法获取OAID");
        } else {
            this.initOAID();
        }
    }

    public void initOAID() {
        AdLogUtils.d("调用工信部OAID SDK获取OAID");
        this.threadPoolExecutor.execute(new Runnable() {
            public void run() {
                OAIDUtils.loadOAID(context, onOAIDLoadCallback);
            }
        });

    }

    public void setIds(String oaid) {
        if (!TextUtils.isEmpty(oaid)) {
            this.mOAID = oaid;
        } else {
            this.mOAID = "";
        }

//        if (!TextUtils.isEmpty(vaid)) {
//            this.mVAID = vaid;
//            SPManager.getInstance().putString(SP_ADBIX_AD_VAID_FILENAME, SP_ADBIX_AD_VAID, vaid);
//        } else {
//            this.mVAID = "";
//        }
//
//        if (!TextUtils.isEmpty(aaid)) {
//            this.mAAID = aaid;
//            SPManager.getInstance().putString(SP_ADBIX_AD_AAID_FILENAME, SP_ADBIX_AD_AAID, aaid);
//        } else {
//            this.mAAID = "";
//        }

    }

    public String getOAID() {
        return this.mOAID;
    }

    public String getVAID() {
        return this.mVAID;
    }

    public String getAAID() {
        return this.mAAID;
    }

    private ThreadPoolExecutor createThreadPoolExecutor() {
        return new ThreadPoolExecutor(2, 10, 20L, TimeUnit.SECONDS, new LinkedBlockingQueue(16), new DiscardOldestPolicy());
    }

    public boolean isDebug() {
        return this.isDebug;
    }

    public Context getContext() {
        return this.context;
    }
}
